import { defineStore } from 'pinia'
import { IDateType, ImagePathEnum } from '@/types'
import type { IDialog } from 'vue3-common/types'
import { IHandleApi } from 'vue3-common/types'
import { ElMessage } from 'element-plus'
import { getCurrentMonth, getCurrentYear, getStartEndDate } from 'vue3-common/utils/dateUtil'
import { ITravelRecord, ITravelSpot, ITravelStats } from '@/types/travel'
import {
  addTravelRecordApi, addTravelSpotApi, deleteTravelRecordApi,
  deleteTravelSpotApi, queryTravelCategoryApi,
  queryTravelRecordApi, queryTravelSpotApi, queryTravelSpotListApi,
  queryTravelSpotNameListApi, queryTravelStatsApi,
  updateTravelRecordApi, updateTravelSpotApi
} from '@/apis/travel'
import { isMobile } from 'vue3-common/utils/layoutUtil'

export const useTravelStore = defineStore('travel', {
  state: () => ({
    travelSpotList: [] as ITravelSpot[],
    currentTravelSpot: {
      id: 0,
      name: '',
      area: ['', ''],
      address: '',
      price: 0,
      recommendRate: 0,
      traffic: '',
      router: '',
      foods: [''],
      projects: [''],
      remark: '',
      recordList: []
    } as ITravelSpot,
    travelRecordList: [] as ITravelRecord[],
    currentTravelRecord: {
      id: 0,
      name: '',
      area: [],
      date: '',
      persons: [],
      imageList: [],
      videoList: []
    } as ITravelRecord,
    travelSpotNameList: [] as string[],
    querySpot: {
      category: ''
    },
    queryRecord: {
      year: getCurrentYear(),
      month: getCurrentMonth()
    },
    queryDateType: 'month' as IDateType,
    queryDateList: [] as string[],
    travelSpotApiType: 'ADD' as IHandleApi,
    travelSpotDialog: {
      visible: false,
      title: '',
      data: {}
    } as IDialog<ITravelSpot>,
    travelRecordApiType: 'ADD' as IHandleApi,
    travelRecordDialog: {
      visible: false,
      title: '',
      data: {}
    } as IDialog<ITravelRecord>,
    editTravelRecordIndex: 0,
    travelImagePath: ImagePathEnum.TRAVEL,
    travelVideoPath: ImagePathEnum.TRAVEL,
    rateColorList: ['#E2DEDC', '#F7BA2A', '#009FA8'],
    pageInfo: {
      currentPage: 1,
      pageSize: 6,
      totalSize: 1
    },
    isScrollEnd: false,
    travelCategoryList: [] as string[],
    travelStats: {
      spotCount: 0,
      cityCount: 0,
      recordCount: 0
    } as ITravelStats,
    isRefresh: false
  }),
  actions: {
    async queryTravelSpotList() {
      const { currentPage, pageSize } = this.pageInfo
      const result = await queryTravelSpotListApi(currentPage, pageSize, this.querySpot)
      this.travelSpotList = result.records
      this.pageInfo.currentPage = result.current
      this.pageInfo.totalSize = result.total
    },
    async queryMobileTravelSpotList() {
      const { currentPage, pageSize } = this.pageInfo
      const result = await queryTravelSpotListApi(currentPage, pageSize, this.querySpot)
      this.travelSpotList.push(...result.records)
      this.pageInfo.totalSize = result.total
    },
    async queryTravelRecordList() {
      const { year, month } = this.queryRecord
      const date = this.queryDateType === 'year' ? year : month
      this.queryDateList = getStartEndDate(date, this.queryDateType)
      this.travelRecordList = await queryTravelRecordApi(this.queryDateList[0], this.queryDateList[1])
    },
    async queryTravelSpotNameList() {
      this.travelSpotNameList = await queryTravelSpotNameListApi()
    },
    async queryTravelSpot(id: number) {
      this.currentTravelSpot = await queryTravelSpotApi(id)
    },
    async queryTravelStats() {
      this.travelStats = await queryTravelStatsApi()
    },
    async queryTravelCategory() {
      this.travelCategoryList = await queryTravelCategoryApi()
    },
    async handleTravelSpotApi(travelSpot: ITravelSpot) {
      switch (this.travelSpotApiType) {
        case 'ADD':
          await addTravelSpotApi(travelSpot)
          ElMessage.success('新增景点成功')
          break
        case 'UPDATE':
          await updateTravelSpotApi(travelSpot.id as number, travelSpot)
          ElMessage.success('更新景点成功')
          break
        case 'DELETE':
          await deleteTravelSpotApi(travelSpot.id as number)
          ElMessage.success('删除景点成功')
          break
        default:
          break
      }
      this.travelSpotDialog.visible = false
      await this.refreshInfo()
    },
    async handleTravelRecordApi(travelRecord: ITravelRecord) {
      switch (this.travelRecordApiType) {
        case 'ADD':
          await addTravelRecordApi(travelRecord)
          ElMessage.success('新增记录成功')
          break
        case 'UPDATE':
          await updateTravelRecordApi(travelRecord.id as number, travelRecord)
          ElMessage.success('更新记录成功')
          break
        case 'DELETE':
          await deleteTravelRecordApi(travelRecord.id as number)
          ElMessage.success('删除记录成功')
          break
        default:
          break
      }
      this.travelRecordDialog.visible = false

      if (!isMobile()) {
        await this.refreshInfo()
      }
    },
    async refreshInfo() {
      await this.queryTravelSpotList()
      await this.queryTravelRecordList()
      await this.queryTravelStats()
      this.isRefresh = !this.isRefresh
    },
    getEmptyTravelSpot(): ITravelSpot {
      return {
        address: '',
        area: [],
        foods: [],
        id: 0,
        name: '',
        price: 0,
        projects: [],
        recommendRate: 0,
        recordList: [],
        remark: '',
        router: '',
        traffic: ''
      }
    },
    getEmptyTravelRecord(): ITravelRecord {
      return {
        area: [],
        date: '',
        id: 0,
        imageList: [],
        videoList: [],
        name: '',
        persons: []
      }
    }
  }
})
